/*
 * Copyright 2014 Toxic Bakery
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ToxicBakery.viewpager.transformer;

import com.ToxicBakery.viewpager.view.CollPageSlide;

import ohos.agp.components.Component;

/**
 * 动画基础类封装
 *
 * @since 2021-04-12
 */
public abstract class ABaseTransformer implements CollPageSlide.PageTransformer {

    /**
     * 计算最小值
     *
     * @param value 当前值
     * @param mi 比较值
     * @return 返回最小值
     */
    static float min(float value, float mi) {
        return Math.max(value, mi);
    }

    @Override
    public void transformPage(Component component, float position) {
        float clampedPosition = clampPosition(position);
        onPreTransform(component, clampedPosition);
        onTransform(component, clampedPosition);
        onPostTransform(component, clampedPosition);
    }

    /**
     * 变化动画
     *
     * @param component 变化组件
     * @param position 变化偏移量
     */
    protected void onPreTransform(Component component, float position) {
        float width = (float) component.getWidth();
        component.setRotation(0.0F);
        component.setScaleX(1.0F);
        component.setScaleY(1.0F);
        component.setPivotX(0.0F);
        component.setPivotY(0.0F);
        component.setTranslationY(0.0F);
        component.setTranslationX(isPagingEnabled() ? 0.0F : -width * position);
        if (hideOffscreenPages()) {
            if (position <= -1f || position >= 1f) {
                component.setAlpha(0f);
            } else {
                component.setAlpha(1f);
            }
            component.setEnabled(false);
        } else {
            component.setEnabled(true);
            component.setAlpha(1.0F);
        }
    }

    private float clampPosition(float position) {
        float clampPosition;
        if (position <= -1F) {
            clampPosition = -1.0F;
        } else if (position >= 1F) {
            clampPosition = 1.0F;
        } else if (Float.isNaN(position)) {
            clampPosition = 0.0F;
        } else {
            clampPosition = position;
        }
        return clampPosition;
    }

    /**
     * 判断是否开启动画
     *
     * @return true 开启动画  false 不开启动画
     */
    public boolean isPagingEnabled() {
        return false;
    }

    /**
     * 隐藏动画开关
     *
     * @return true 开  false 关
     */
    protected boolean hideOffscreenPages() {
        return true;
    }

    /**
     * 做多个动画
     *
     * @param component 动画组件
     * @param position 偏移量
     */
    protected void onPostTransform(Component component, float position) {
    }

    /**
     * 做动画
     *
     * @param component 动画组件
     * @param position 偏移量
     */
    protected abstract void onTransform(Component component, float position);
}
